<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta name="referrer" content="same-origin">
<meta name="referrer" content="no-referrer" />








<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="DDD," />










<meta name="description" content="&amp;emsp;&amp;emsp;在使用领域驱动设计（DDD）做了几个项目之后（实际上更多的是借鉴DDD的战术设计），感觉自己更加深入地了解了DDD以及DDD相关架构模式（特别是六边形架构）的一些思想，再结合项目之后的归纳和总结，不论优点和缺点，总归有了一些收获，于是决定写一个笔记，将整个实践过程中采纳的方案、要点、还有一些弯弯绕绕的东西都记录下来。">
<meta name="keywords" content="DDD">
<meta property="og:type" content="article">
<meta property="og:title" content="DDD实践笔记">
<meta property="og:url" content="http://yoursite.com/2021/01/17/DDD实践笔记/index.html">
<meta property="og:site_name" content="矩阵编程">
<meta property="og:description" content="&amp;emsp;&amp;emsp;在使用领域驱动设计（DDD）做了几个项目之后（实际上更多的是借鉴DDD的战术设计），感觉自己更加深入地了解了DDD以及DDD相关架构模式（特别是六边形架构）的一些思想，再结合项目之后的归纳和总结，不论优点和缺点，总归有了一些收获，于是决定写一个笔记，将整个实践过程中采纳的方案、要点、还有一些弯弯绕绕的东西都记录下来。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://yoursite.com/2021/01/17/DDD实践笔记/ddd_hierarchy.jpg">
<meta property="og:image" content="http://yoursite.com/2021/01/17/DDD实践笔记/project_%20architecture.jpg">
<meta property="og:image" content="http://yoursite.com/2021/01/17/DDD实践笔记/domain-core.jpg">
<meta property="og:updated_time" content="2021-01-17T10:39:36.565Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="DDD实践笔记">
<meta name="twitter:description" content="&amp;emsp;&amp;emsp;在使用领域驱动设计（DDD）做了几个项目之后（实际上更多的是借鉴DDD的战术设计），感觉自己更加深入地了解了DDD以及DDD相关架构模式（特别是六边形架构）的一些思想，再结合项目之后的归纳和总结，不论优点和缺点，总归有了一些收获，于是决定写一个笔记，将整个实践过程中采纳的方案、要点、还有一些弯弯绕绕的东西都记录下来。">
<meta name="twitter:image" content="http://yoursite.com/2021/01/17/DDD实践笔记/ddd_hierarchy.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2021/01/17/DDD实践笔记/"/>





  <title>DDD实践笔记 | 矩阵编程</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?7d29e6fba8e07d1a8af2414a20e5f2a6";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">矩阵编程</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-主页">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-主页"></i> <br />
            
            主页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-分类"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-所有文章">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-文章"></i> <br />
            
            所有文章
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/01/17/DDD实践笔记/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="matrix">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="矩阵编程">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">DDD实践笔记</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-01-17T18:39:36+08:00">
                2021-01-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/DDD/" itemprop="url" rel="index">
                    <span itemprop="name">DDD</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2021/01/17/DDD实践笔记/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2021/01/17/DDD实践笔记/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&emsp;&emsp;在使用领域驱动设计（DDD）做了几个项目之后（实际上更多的是借鉴DDD的战术设计），感觉自己更加深入地了解了DDD以及DDD相关架构模式（特别是六边形架构）的一些思想，再结合项目之后的归纳和总结，不论优点和缺点，总归有了一些收获，于是决定写一个笔记，将整个实践过程中采纳的方案、要点、还有一些弯弯绕绕的东西都记录下来。<br><a id="more"></a></p>
<h3 id="架构设计"><a href="#架构设计" class="headerlink" title="架构设计"></a>架构设计</h3><p>&emsp;&emsp;架构设计主要是探讨四层架构模式以及之前项目的一些模块设计，如果对DDD的一些概念或者术语还不是很清楚，可以参考<a href="/2020/12/05/领域驱动设计（DDD）笔记/">领域驱动设计（DDD）笔记</a>。在做架构设计的时候，不管最后使用了何种方式，我们只需要记住几个小目标，具体参照整洁架构（The Clean Architecture）：</p>
<ul>
<li><strong>框架分离</strong> 业务不受框架的限制</li>
<li><strong>可测试性</strong> 业务逻辑独立测试，不依赖于UI、数据库、Web服务等其他外部条件</li>
<li><strong>独立于UI</strong> UI层的替换和修改不会修改其他系统模块</li>
<li><strong>独立于DB</strong> 数据库存储的独立、可替换性</li>
<li><strong>与外部结构分离</strong> 系统业务逻辑不需要知道任何外部结构</li>
</ul>
<h5 id="分层架构"><a href="#分层架构" class="headerlink" title="分层架构"></a>分层架构</h5><p>&emsp;&emsp;分层架构是所有架构的始祖，DDD分层架构经过一些优化和解耦后形成了如下的四层架构：<img src="/2021/01/17/DDD实践笔记/ddd_hierarchy.jpg" alt><br>在传统的四层架构中，基础层是被其他层依赖的，这种依赖方式按照分层架构的思想来说，基础设施层才应该是核心，但实际上不是，所以传统的四层架构是有问题的。后来传统的四层架构又通过依赖倒置的方式转变为以领域层为核心的四层架构，我们可以通过在领域层中定义接口，然后在基础设施层实现的方式来解耦基础层。</p>
<h5 id="模块设计"><a href="#模块设计" class="headerlink" title="模块设计"></a>模块设计</h5><p>&emsp;&emsp;按照DDD四层架构的思想，我们的项目分为以下四个模块：</p>
<ul>
<li>infrastructure 基础设施层</li>
<li>interface 接口层</li>
<li>application 应用层</li>
<li>domain 领域层</li>
</ul>
<p>很多DDD项目也是基于此进行模块或者包的设计，比如领域层的实现放在 <strong>org.matrxi.ddd.demo.domain</strong>包下，这种是以包的方式进行模块管理，适合比较小的项目，当然为了更加细致地拆分不同模块，我们通常使用Module化的方式拆分，比如单独会有一个domain的模块来存放domain相关的业务。不过需要注意，因为领域层可能不单单只有一种领域（核心域），可能包含了核心域、子域、支撑域等等（从项目团队维护的角度考虑，初期不会拆分的这么细致）；或者从 interface 接口层考虑，也不单单只有Web接口，可能会以微服务化的方式提供RPC服务，或者以消息中间件的方式来消费数据。所以单一的模块化管理也可能会出现因为模块数量迅速膨胀而导致整个项目模块混乱的情况。基于此考虑，我觉得DDD的四层不应该作为直接模块来使用，而应该作为模块的分类，如下：<br><img src="/2021/01/17/DDD实践笔记/project_ architecture.jpg" alt></p>
<p>&emsp;&emsp;使用四层架构作为顶层模块分类，具体的模块放在各自层的文件夹中，比如domain下可以有多个领域模块，infrastructure下也会有多种数据库仓储和缓存的实现，这样整个项目结构看起来会更加的清晰。</p>
<h3 id="战略设计"><a href="#战略设计" class="headerlink" title="战略设计"></a>战略设计</h3><p>&emsp;&emsp;因为DDD的战略设计和业务之间强关联，每一种业务各自的侧重点大相径庭，所以很难进行详尽的总结和表述，我没法记录太多关于战略设计的东西，我觉得主要还是要对业务有全局的视野和整体把控，这样才能理解领域中核心域、子域、支撑域、通用子域的一些概念，区分轻重缓急。</p>
<h3 id="案例分析"><a href="#案例分析" class="headerlink" title="案例分析"></a>案例分析</h3><p>&emsp;&emsp;规划好了架构和项目模块，我们来举个例子，以账户转账功能作为整个项目的需求点，假设我们拆解出如下步骤：</p>
<ol>
<li>A账户转钱给B账户，先从数据库中查找到对应的A/B账户；</li>
<li>扣款校验，确保A账户有钱；</li>
<li>A账户出账，B账户进账，保存到数据库；</li>
<li>发送转账成功事件，用于对账审计；</li>
</ol>
<blockquote>
<p>以上只是虚拟的转账业务功能，不要较真。</p>
</blockquote>
<h5 id="domain"><a href="#domain" class="headerlink" title="domain"></a>domain</h5><p>&emsp;&emsp;首先在domain/core模块下新建<strong>Account</strong>:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Account</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 账户ID</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> String id;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 可用余额</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> Money available;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 出账</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> money</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">withdraw</span><span class="params">(Money money)</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 进账</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> money</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deposit</span><span class="params">(Money money)</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>Account是一个很简单的账户实体，其中又包含了余额值对象，以及一些账户的领域行为。这里不论Account建模是否合适，在项目初期我们就应该形成DDD领域建模的理念，不要老是将关注的重点放在数据库层，不要急急忙忙地去设计数据库的列和关联，然后再将数据库的列回写到领域对象中。这会导致领域层实体（Entity）只是一个包含大量getter和setter方法而缺乏实际行为的贫血对象，这样的做法不是DDD的做法，DDD应该优先规划好实体、值对象，使用更加面向对象的方式去建模。</p>
<blockquote>
<p>领域建模第一步，将开发的关注重点转移到业务领域建模上，而不是数据实现。</p>
</blockquote>
<p>&emsp;&emsp;有了Account实体是不够的，因为一个转账功能需要两个账户，所以需要借助领域服务来实现，我们又要在domain/core模块新建<strong>TransferService</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TransferService</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 转账</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> money</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> withdrawAccount</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> depositAccount</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">trans</span><span class="params">(Money money, Account withdrawAccount, Account depositAccount)</span></span>&#123;</span><br><span class="line">        withdrawAccount.withdraw(money);</span><br><span class="line">        depositAccount.deposit(money);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>整个TransferService的功能设计非常简单，只是扣款和进款，至于校验账户有效性的功能我觉得应该交给账户本身，比如账户在withdraw的时候本身就应该考虑是否有足够的余额进行出账，如果没有可以通过异常的方式进行中断处理。<br>&emsp;&emsp;做完了核心的转账功能，下一步我们就应该考虑资源库（Repository），比如如何获取Account，注意现在还不是考虑具体数据库存储层问题的时候，不论是mysql或者mongodb都不是重点。严格来讲，只有聚合才拥有资源库，不过我们不讨论聚合，可以简单点就把Account当做一个聚合来看，在domain/core模块新建<strong>AccountRepository</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AccountRepository</span></span>&#123;</span><br><span class="line">    <span class="function">Account <span class="title">getOne</span><span class="params">(String accountId)</span></span>;</span><br><span class="line">    <span class="function">Account <span class="title">save</span><span class="params">(Account account)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>在领域层的资源库我们只写接口，不写具体实现，具体的实现放在基础设施层（infrastructure）。这里如果再优化一点，可以对整个资源库做一个简单的抽象封装，先封装出一套Repository基类，这样的话使用起来会更加高效，比如可以参照Spring data jpa：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 资源库基类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &lt;T&gt;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &lt;ID&gt;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Repository</span>&lt;<span class="title">T</span>, <span class="title">ID</span>&gt; </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 资源库增删改查</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &lt;T&gt;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &lt;ID&gt;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">CrudRepository</span> &lt;<span class="title">T</span>, <span class="title">ID</span>&gt; <span class="keyword">extends</span> <span class="title">Repository</span>&lt;<span class="title">T</span>, <span class="title">ID</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    &lt;S extends T&gt; <span class="function">S <span class="title">save</span><span class="params">(S var1)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function">T <span class="title">getOne</span><span class="params">(ID var1)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function">Iterable&lt;T&gt; <span class="title">findAll</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">delete</span><span class="params">(T var1)</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 资源库分页</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &lt;T&gt;</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> &lt;ID&gt;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">PagingAndSortingRepository</span> &lt;<span class="title">T</span>, <span class="title">ID</span>&gt; <span class="keyword">extends</span> <span class="title">CrudRepository</span>&lt;<span class="title">T</span>, <span class="title">ID</span>&gt;</span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function">Iterable&lt;T&gt; <span class="title">findAll</span><span class="params">(Sort var1)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function">Page&lt;T&gt; <span class="title">findAll</span><span class="params">(Pageable var1)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>具体的<strong>CrudRepository</strong>或者<strong>PagingAndSortingRepository</strong>可以按照实际需求进行分装，这样AccountRepository可以直接继承<strong>CrudRepository</strong>或者<strong>PagingAndSortingRepository</strong>，在基础设施层统一实现可以减少很大一部分增删改查的工作量。以上整个结构如下：<br><img src="/2021/01/17/DDD实践笔记/domain-core.jpg" alt></p>
<p>&emsp;&emsp;总结一下，核心领域层的实现，没有依赖任何第三方的框架，包含了所有核心的业务，并且可以独立测试。</p>
<h5 id="interface"><a href="#interface" class="headerlink" title="interface"></a>interface</h5><p>&emsp;&emsp;用户接口层一般存放和终端用户相关的代码和模块，在interface/api模块新建<strong>TransferApi</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">TransferApi</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">trans</span><span class="params">(TransferDTO transferDTO)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>api模块只会封装一层用户交互接口而不写具体实现，而具体的实现在infrastructure/web中实现，这是为了从可以提供多种接口的交互形式上考虑，比如web接口、rpc接口等。另外interface层也可以接受消息队列或者定时任务的输入，可以直接调用application层的代码进行业务处理。<br>&emsp;&emsp;interface/boot是整个项目的启动入口，添加<strong>BootApplication</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BootApplication</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication.run(BootApplication.class, args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h5 id="application"><a href="#application" class="headerlink" title="application"></a>application</h5><p>&emsp;&emsp;应用层在DDD中是很薄的一层，相当于传统分层模型中的service层但是又不同于传统分层模型中service层，首先应用层没有业务逻辑，只做业务编排、事务、流程等。在application/api模块下新建几个数据传输类（DTO）：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 省略getter/setter</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MoneyDTO</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> amount;</span><br><span class="line">    <span class="keyword">private</span> String currency;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AccountDTO</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String id;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TransferDTO</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> MoneyDTO money;</span><br><span class="line">    <span class="keyword">private</span> AccountDTO account;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>这里省略了具体的参数，按照既定的数据传输格式来，为什么要单独新建一个application/api模块来存放对应的DTO文件？其实主要也是为了rpc接口的考虑，interface/api依赖application/api，可以提供最轻量级的RPC包引用。继续在application/service层新建<strong>AccountTransferService</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AccountTransferService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> AccountRepository accountRepository;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> TransferService transferService;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> EventPublisher eventPublisher;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">AccountTransferService</span><span class="params">(AccountRepository accountRepository,</span></span></span><br><span class="line"><span class="function"><span class="params">                                  TransferService transferService,</span></span></span><br><span class="line"><span class="function"><span class="params">                                  EventPublisher eventPublisher)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.accountRepository = accountRepository;</span><br><span class="line">        <span class="keyword">this</span>.transferService = transferService;</span><br><span class="line">        <span class="keyword">this</span>.eventPublisher = eventPublisher;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">trans</span><span class="params">(MoneyDTO moneyDTO,</span></span></span><br><span class="line"><span class="function"><span class="params">                      AccountDTO withdrawAccountDTO,</span></span></span><br><span class="line"><span class="function"><span class="params">                      AccountDTO depositAccountDTO)</span> </span>&#123;</span><br><span class="line">        Account withdrawAccount = accountRepository.getOne(withdrawAccountDTO.getId());</span><br><span class="line">        Account depositAccount = accountRepository.getOne(depositAccountDTO.getId());</span><br><span class="line">        Money money = MoneyMapper.INSTANCE.convert(moneyDTO);</span><br><span class="line">        transferService.trans(money, withdrawAccount, depositAccount);</span><br><span class="line">        accountRepository.save(withdrawAccount);</span><br><span class="line">        accountRepository.save(depositAccount);</span><br><span class="line">        eventPublisher.publishEvent(<span class="keyword">new</span> TransCompletedEvent());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>整个AccountTransferService没有任何业务逻辑，只是调用domain层的TransferService方法进行转账功能，最后持久化账户到资源库中。</p>
<blockquote>
<p>application有很多DTO和DO的转换，推荐使用mapstruct。<br><strong>TransCompletedEvent</strong>和<strong>EventPublisher</strong>定义在domain层，但是由application层发出，主要是考虑到一些事务一致性的影响。</p>
</blockquote>
<h5 id="infrastructure"><a href="#infrastructure" class="headerlink" title="infrastructure"></a>infrastructure</h5><p>&emsp;&emsp;现在可以考虑具体的存储层实现了，假设我们使用mysql作为存储层，在infrastructure/repo-mysql模块下新建<strong>AccountPO</strong>作为数据库持久层对象：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AccountPO</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String id;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> available;</span><br><span class="line">    <span class="keyword">private</span> String currency;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>然后再新建<strong>AccountRepositoryImpl</strong>作为资源库实现：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Repository</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AccountRepositoryImpl</span> <span class="keyword">implements</span> <span class="title">AccountRepository</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> AccountMapper accountMapper;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">AccountRepositoryImpl</span><span class="params">(AccountMapper accountMapper)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.accountMapper = accountMapper;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Account <span class="title">getOne</span><span class="params">(String accountId)</span> </span>&#123;</span><br><span class="line">        AccountPO accountPO = accountMapper.selectById(accountId);</span><br><span class="line">        <span class="keyword">return</span> AccountPOMapper.INSTANCE.convert(accountPO);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Account <span class="title">save</span><span class="params">(Account account)</span> </span>&#123;</span><br><span class="line">        AccountPO accountPO = AccountPOMapper.INSTANCE.convert(account);</span><br><span class="line">        accountMapper.save(accountPO);</span><br><span class="line">        <span class="keyword">return</span> account;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>可以使用mybatis的mapper作为dao层实现（也可以使用jpa），如果做过CrudRepository的封装，同样可以通过实现CrudRepositoryImpl实现基础增删改查的功能。<br>&emsp;&emsp;存储层也准备好了，接下来可以在infrastructure/web模块添加web层的实现<strong>AccountTransferController</strong>:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/account"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AccountTransferController</span> <span class="keyword">implements</span> <span class="title">TransferApi</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> AccountTransferService accountTransferService;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">AccountTransferController</span><span class="params">(AccountTransferService accountTransferService)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.accountTransferService = accountTransferService;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostMapping</span>(<span class="string">"trans"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">trans</span><span class="params">(@RequestBody TransferDTO transferDTO)</span> </span>&#123;</span><br><span class="line">        accountTransferService.trans(transferDTO.getMoney(),</span><br><span class="line">                transferDTO.getWithdrawAccount(),</span><br><span class="line">                transferDTO.getDepositAccount());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>rpc层的实现同理，甚至可以不写rpc实现而使用AccountTransferController进行重用，做好一些RPC对应的配置后提供整个interface/api包给第三方调用者即可。</p>
<h5 id="集成环境"><a href="#集成环境" class="headerlink" title="集成环境"></a>集成环境</h5><p>&emsp;&emsp;所有的业务代码以及用户接口、数据库存储都已经准备完毕，可以通过Spring很方便地实现依赖反转，那么如何将其他模块集成到interface/boot模块中以启动整个程序呢？推荐使用spring-boot-starter，在对应的基础层模块中引入：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-configuration-processor<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>比如集成web模块，可以在infrastructure/web模块中添加<strong>WebConfig</strong>:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@ComponentScan</span>(<span class="string">"org.matrix.ddd.demo.web"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WebConfig</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>然后在infrastructure/web模块的resources文件夹下新建<strong>META-INF/spring.factories</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.matrix.ddd.demo.web.WebConfig</span><br></pre></td></tr></table></figure></p>
<p>只要将infrastructure/web的依赖添加到interface/boot下，用户就可以直接访问其下web接口了。同理，数据库层也可以照此操作，无论是mysql还是mongodb实现，可以任意切换，只要更改pom依赖，其他什么都不需要改，完美实现仓储层的动态插拔。</p>
<h3 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h3><h5 id="文档"><a href="#文档" class="headerlink" title="文档"></a>文档</h5><p>&emsp;&emsp;通常使用swagger作为文档，因为需要对DTO层的一些字段提供说明，所以需要在application/api层引入swagger依赖，这里也是稍有瑕疵的地方，不过只需要依赖<strong>swagger-annotations</strong>即可。</p>
<h5 id="领域服务"><a href="#领域服务" class="headerlink" title="领域服务"></a>领域服务</h5><p>&emsp;&emsp;因为领域层不依赖于任何框架，那么如何将领域服务交给Spring Bean管理？相对简单便捷的方式，可以在domain/core中自定义注解<strong>DomainService</strong>:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Target</span>(&#123;ElementType.TYPE&#125;)</span><br><span class="line"><span class="meta">@Retention</span>(RetentionPolicy.RUNTIME)</span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> DomainService &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>然后在需要的domain service上加上此注解，比如前面的<strong>TransferService</strong>。最后在application/service层使用Spring <strong>BeanDefinitionRegistryPostProcessor</strong>扫描所有的<strong>DomainService</strong>：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ServiceConfig</span> <span class="keyword">implements</span> <span class="title">BeanDefinitionRegistryPostProcessor</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> String DOMAIN_SERVICE_PACKAGE = <span class="string">"org.matrix.ddd.demo.domain.core"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postProcessBeanDefinitionRegistry</span><span class="params">(BeanDefinitionRegistry registry)</span> <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        DomainServiceScanner scanner = <span class="keyword">new</span> DomainServiceScanner(registry);</span><br><span class="line">        scanner.scan(DOMAIN_SERVICE_PACKAGE);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">postProcessBeanFactory</span><span class="params">(ConfigurableListableBeanFactory beanFactory)</span> <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">DomainServiceScanner</span> <span class="keyword">extends</span> <span class="title">ClassPathBeanDefinitionScanner</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">DomainServiceScanner</span><span class="params">(BeanDefinitionRegistry registry)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">super</span>(registry, <span class="keyword">false</span>);</span><br><span class="line">            addIncludeFilter(<span class="keyword">new</span> AnnotationTypeFilter(DomainService.class));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h5 id="DTO归属"><a href="#DTO归属" class="headerlink" title="DTO归属"></a>DTO归属</h5><p>&emsp;&emsp;关于DTO应该放在application还是interface是我比较纠结的问题，目前我的做法是放到了application层，但是也导致了swagger依赖侵入application层的一些问题，显得并不那么优雅。在<a href="https://stackoverflow.com/questions/31438286/ddd-which-layer-dto-should-be-implemented" target="_blank" rel="noopener">DDD - which layer DTO should be implemented</a>中高票回答推荐将DTO放在Domain Service Layer（应该相对于application层），不过在<a href="https://github.com/citerus/dddsample-core" target="_blank" rel="noopener">DDDSample</a>中DTO归属于用户接口层，只是目前未尝试过。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/DDD/" rel="tag"># DDD</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/12/27/从mybatis到mybatis-spring-boot-starter（二）/" rel="next" title="从mybatis到mybatis-spring-boot-starter（二）">
                <i class="fa fa-chevron-left"></i> 从mybatis到mybatis-spring-boot-starter（二）
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">matrix</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">47</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#架构设计"><span class="nav-number">1.</span> <span class="nav-text">架构设计</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#分层架构"><span class="nav-number">1.0.1.</span> <span class="nav-text">分层架构</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#模块设计"><span class="nav-number">1.0.2.</span> <span class="nav-text">模块设计</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#战略设计"><span class="nav-number">2.</span> <span class="nav-text">战略设计</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#案例分析"><span class="nav-number">3.</span> <span class="nav-text">案例分析</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#domain"><span class="nav-number">3.0.1.</span> <span class="nav-text">domain</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#interface"><span class="nav-number">3.0.2.</span> <span class="nav-text">interface</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#application"><span class="nav-number">3.0.3.</span> <span class="nav-text">application</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#infrastructure"><span class="nav-number">3.0.4.</span> <span class="nav-text">infrastructure</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#集成环境"><span class="nav-number">3.0.5.</span> <span class="nav-text">集成环境</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#问题"><span class="nav-number">4.</span> <span class="nav-text">问题</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#文档"><span class="nav-number">4.0.1.</span> <span class="nav-text">文档</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#领域服务"><span class="nav-number">4.0.2.</span> <span class="nav-text">领域服务</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#DTO归属"><span class="nav-number">4.0.3.</span> <span class="nav-text">DTO归属</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">matrix</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>
<div class=BbeiAn-info">
  浙ICP备 -
  <a href="http://www.beian.miit.gov.cn">18040498号</a>
  </a>
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  

    
      <script id="dsq-count-scr" src="https://luyun.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://yoursite.com/2021/01/17/DDD实践笔记/';
          this.page.identifier = '2021/01/17/DDD实践笔记/';
          this.page.title = 'DDD实践笔记';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://luyun.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
